package com.zhekou.util;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * User: fang
 * Date: 12-7-5
 * Time: 下午3:54
 */
@SuppressWarnings({"unchecked"})
public class ListUtil {
    public static boolean isNotEmpty(List list) {
        return list != null && list.size() > 0;
    }

    public static boolean isEmpty(List list) {
        return list == null || list.size() == 0;
    }

    public static boolean isExist(List list, String property, Object value) {
        return getObject(list, property, value) != null;
    }

    public static boolean containsIgnoreCase(List<String> list,String s){
        for(String item:list){
            if(StringUtils.equalsIgnoreCase(item,s)){
                return true;
            }
        }
        return false;
    }

    public static Object getObject(List list, String property, Object value) {
        if (isNotEmpty(list)) {
            for (Object o : list) {
                Object v = ReflectionUtils.getFieldValue(o, property);
                if (v != null) {
                    if (v instanceof String) {
                        if (((String) v).equalsIgnoreCase(value.toString())) {
                            return o;
                        }
                    } else {
                        if (v.equals(value)) {
                            return o;
                        }
                    }
                }
                if (v != null && v.equals(value)) {
                    return o;
                }
            }
        }
        return null;
    }

}
